package cn.guozeyiblog.client;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.guozeyiblog.dbhelper.DBHelper;
import net.youmi.android.AdManager;
/**
 * 启动页面
 * @author kutear
 *
 */
public class WelcomeActivity extends Activity {
	private static String DB_CITY="/data/data/cn.guozeyiblog.client/databases/City.db";
	private LinearLayout mainlayout;
	private ImageView logoImageView;
	private LinearLayout logolayout;
	private TextView logotextview;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		initView();
		
		AdManager.getInstance(WelcomeActivity.this).init("b92039c8e7a9b59e","c81cda4c33abef3c",false);
		/**
		 * 背景随机
		 */
		int random = (int) (Math.random()*4+1);
		switch (random) {
		case 1:
			mainlayout.setBackgroundResource(R.drawable.welcome_photo_01);
			break;
		case 2:
			mainlayout.setBackgroundResource(R.drawable.welcome_photo_02);
			break;
		case 3:
			mainlayout.setBackgroundResource(R.drawable.welcome_photo_03);
			break;
		case 4:
			mainlayout.setBackgroundResource(R.drawable.welcome_photo_04);
			break;
		default:
			mainlayout.setBackgroundResource(R.drawable.welcome_photo_04);
			break;
		}
		/**
		 * 为logo设置动画
		 */
		DisplayMetrics dm=new DisplayMetrics();
		WindowManager manager = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		manager.getDefaultDisplay().getMetrics(dm);
		int width=dm.widthPixels;
		Log.v("Width", width+"");
		Animation imageviewAnimation = new TranslateAnimation(-(width+20)/2, 0, 0, 0);
		Animation textviewAnimation = new TranslateAnimation((width+20)/2, 0, 0, 0);
		imageviewAnimation.setDuration(1000);
		textviewAnimation.setDuration(1000);
		logoImageView.startAnimation(imageviewAnimation);
		logotextview.startAnimation(textviewAnimation);
		File f = new File("/data/data/cn.guozeyiblog.client/databases");
		if (!f.exists()) {
			f.mkdir();
		}
		Log.v("DB_start", "------------------------------------------------------");
		DBHelper helper = new DBHelper(getApplicationContext(), "Detail.db");
		SQLiteDatabase db = helper.getWritableDatabase(); //创建数据库
		db.close();
		//对动画完毕的监听，动画完毕页面跳转
		textviewAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				//导入全国城市数据库
				File file = new File(DB_CITY);
				if (!file.exists()) {
					try {
						InputStream is = getAssets().open("citychina.db");
						FileOutputStream os = new FileOutputStream(file);
						byte[] data = new byte[1024];
						while (is.read(data)!=-1) {
							os.write(data);
						}
						os.flush();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.v("Welcome", "写入数据成功");
				}
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				//页面跳转
				//Toast.makeText(getApplicationContext(), "Mian Activity", 0).show();
				Intent intent = new Intent(getApplicationContext(), MainAll.class);
				startActivity(intent);
				overridePendingTransition(R.anim.enter, R.anim.outer);
				finish();
			}
		});
	}
	private void initView(){
		mainlayout = (LinearLayout) findViewById(R.id.mainlayout);
		logoImageView = (ImageView) findViewById(R.id.logoImageView);
		logolayout = (LinearLayout) findViewById(R.id.logo);
		logotextview = (TextView) findViewById(R.id.logodescription);
	}
} 